package com.polaris.live.ui.luckybag.fragment.dialog

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.os.bundleOf
import androidx.fragment.app.viewModels
import androidx.viewpager2.widget.ViewPager2
import com.polaris.live.R
import com.polaris.live.common.adapter.ViewPagerAdapter
import com.polaris.live.common.ext.dp
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.databinding.FragmentDialogLuckyBagMessageBinding
import com.polaris.live.dialog.luckybag.LuckyBagDescDialog
import com.polaris.live.dialog.luckybag.LuckyBagSendDialog
import com.polaris.live.ui.luckybag.fragment.LuckyBagMessageFragment
import com.polaris.live.ui.message.fragment.dialog.KeyboardBottomDialogFragment
import com.polaris.live.utils.XPopupUtils
import com.polaris.live.utils.fragment.showNewDialogFragment
import com.polaris.live.viewmodel.luckybag.LuckyBagViewModel

/**
 * LuckyBagMessageDialogFragment
 *
 * @author Created by lucas on 2024/6/11 02:44
 * @since 1.0.0
 */
class LuckyBagMessageDialogFragment : KeyboardBottomDialogFragment() {

    private lateinit var mBinding: FragmentDialogLuckyBagMessageBinding

    private val mViewModel by viewModels<LuckyBagViewModel>()

    private val mLiveId by lazy {
        arguments?.getLong("liveId", -1L)?.let {
            if (it == -1L) {
                null
            } else {
                it
            }
        }
    }

    private val mFragments by lazy {
        listOf(
            LuckyBagMessageFragment().apply {
                arguments = bundleOf("liveId" to mLiveId, "type" to LuckyBagMessageFragment.ALL)
            },
            LuckyBagMessageFragment().apply {
                arguments = bundleOf("liveId" to mLiveId, "type" to LuckyBagMessageFragment.CLAIMABLE)
            }
        )
    }

    override fun getTopMargin(): Int {
        return 148.dp
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View {
        return FragmentDialogLuckyBagMessageBinding.inflate(inflater, container, false).let {
            mBinding = it

            it.root
        }
    }

    override fun setStyle() {
        setStyle(
            STYLE_NO_FRAME,
            R.style.DialogFragmentBottomDialog_Keyboard_Single
        )
    }

    override fun onShowKeyboard() {
    }

    override fun onHideKeyboard() {
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        initView()
        initListener()

        mViewModel.startObserveState()
    }

    private fun initView() {
        mBinding.apply {
            vpPage.offscreenPageLimit = 2
            vpPage.adapter = ViewPagerAdapter(mFragments, this@LuckyBagMessageDialogFragment)
            vpPage.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
                override fun onPageSelected(position: Int) {
                    super.onPageSelected(position)
                    when (position) {
                        0 -> changeSelect(LuckyBagMessageFragment.ALL)
                        1 -> changeSelect(LuckyBagMessageFragment.CLAIMABLE)
                    }
                }
            })
        }
    }

    private fun initListener() {
        mBinding.apply {
            bvTitle.setOnLeftClientListener {
                XPopupUtils.showPopup(
                    LuckyBagDescDialog(requireContext()),
                    { hasShadowBg(true) },
                    lifecycle = lifecycle
                )
            }
            bvTitle.setOnRightClientListener {
                childFragmentManager.showNewDialogFragment(
                    LuckyBagHistoryDialogFragment(),
                    LuckyBagHistoryDialogFragment.FRAGMENT_TAG
                )
            }
            llBtn.setOnSingleClickListener {
                XPopupUtils.showPopup(
                    LuckyBagSendDialog(requireContext(), mViewModel, mLiveId),
                    { hasShadowBg(true) },
                    lifecycle = lifecycle
                )
            }
            tvBarAll.setOnSingleClickListener {
                vpPage.currentItem = 0
                changeSelect(LuckyBagMessageFragment.ALL)
            }
            tvBarClaimable.setOnSingleClickListener {
                vpPage.currentItem = 1
                changeSelect(LuckyBagMessageFragment.CLAIMABLE)
            }

            changeSelect(LuckyBagMessageFragment.ALL)
        }
    }

    private fun changeSelect(type: Int) {
        when (type) {
            LuckyBagMessageFragment.ALL -> {
                mBinding.tvBarAll.isSelected = true
                mBinding.tvBarClaimable.isSelected = false
            }

            LuckyBagMessageFragment.CLAIMABLE -> {
                mBinding.tvBarAll.isSelected = false
                mBinding.tvBarClaimable.isSelected = true
            }
        }
    }

    companion object {

        const val FRAGMENT_TAG = "LuckyBagMessageDialogFragment"
    }
}